package com.practice.communication.aio;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class ServerAcceptHandler implements CompletionHandler<AsynchronousSocketChannel, ServerAsyncHandler> {

	@Override
	public void completed(AsynchronousSocketChannel channel, ServerAsyncHandler serverHandler) {
		//继续接收其它客户端的请求
		Server.clientCount.incrementAndGet();
		System.out.println("连接的客户端数：" + Server.clientCount.get());
		
		serverHandler.serverChannel.accept(serverHandler, this);
		
		//创建新的Buffer
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		
		channel.read(buffer, buffer, new ServerReadHandler(channel));
	}

	@Override
	public void failed(Throwable exc, ServerAsyncHandler serverHandler) {
		exc.printStackTrace();
		serverHandler.latch.countDown();
	}

}
